package com.yjy.fuyao.vod.controller;

import com.aliyun.vod20170321.Client;
import com.aliyun.vod20170321.models.GetVideoPlayAuthRequest;
import com.aliyun.vod20170321.models.GetVideoPlayAuthResponse;
import com.yjy.fuyao.utils.result.R;
import com.yjy.fuyao.vod.client.StatisticsClient;
import com.yjy.fuyao.vod.util.AliyunVodSDKUtils;
import com.yjy.fuyao.vod.util.VodProperties;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags="阿里云视频点播微服务")
@RestController
@RequestMapping("/front/vod/video")
public class VideoController {

    @Resource
    private StatisticsClient statisticsClient;

    @GetMapping("get-play-auth/{videoId}")
    public R getVideoPlayAuth(@PathVariable("videoId") String videoId) throws Exception {

        statisticsClient.addVideoViewNum();

        //获取阿里云存储相关常量
        String accessKeyId = VodProperties.KEY_ID;
        String accessKeySecret = VodProperties.KEY_SECRET;

        //初始化
        Client client = AliyunVodSDKUtils.createClient(accessKeyId, accessKeySecret);

        //请求
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest().setVideoId(videoId);

        //响应
        GetVideoPlayAuthResponse response = client.getVideoPlayAuth(request);

        //得到播放凭证
        String playAuth = response.getBody().getPlayAuth();

        //返回结果
        return R.ok().message("获取凭证成功").data("playAuth", playAuth);
    }
}
